package com.xm_jd.controller.zc;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xm_jd.model.dao.ZcDao;
import com.xm_jd.model.entitys.user;
import com.xm_jd.utils.BaseServlet;
import com.xm_jd.utils.MD5;

/**
 * Servlet implementation class Register
 */
@WebServlet("/register")
public class Register extends BaseServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				this.forward("register");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

			response.setCharacterEncoding("utf-8");
			request.setCharacterEncoding("utf-8");
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			String phone = request.getParameter("phone");

			List<user> name = new ZcDao().Selectname(userName);
			try {
				
				if (password == null && phone == null) {
					if (name.size() > 0) {
						System.out.println(name.size());
						response.getWriter().println("0");
					} else {
						response.getWriter().println("1");
						System.out.println(name.size());
					}

				} else {
					// 鏂板涓�涓敤鎴�
					String newpwd = MD5.md5(password);
					new ZcDao().addUonHu(userName, newpwd, phone);	
				}
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}

}
